var { ReturnModel } = require('../libs/resmodel');
var { orderList, orderQueryone, signUp, signIn, order_edit,orderById,orderExport,order_notice, order_back } = require('../services/order');
var { payto } = require('../libs/wxpay');

/**
* 订单列表
* @api {post} /order/list 订单列表
* @apiDescription 订单列表
* @apiName list
* @apiParam (参数) {Number} pagenum 查询第几页
* @apiParam (参数) {Number} pagesize 每页显示条数
* @apiParam (参数) {String} pcode 用户code，不传查所有订单
* @apiSampleRequest /order/list
* @apiGroup order-订单
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*    {
*     "pagenum": 1,  // 查询第几页
*     "pagesize": 10,  // 每页显示条数
*     "pcode": '290464549598',  // 用户code，不传查所有订单
*    }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": {
*              "list": [
*                   pdata: {"nickname":"xxxx","code":"","avator":""},// 用户信息
*                   title: "活动标题",
*                   ordercode: "订单号",
*                   price: 200,//订单金额,单位元
*                   title: "活动标题",
*                   tourl: "活动详情页面",
*                   imgurl: "活动简版图片",// 用于展示，点击跳转
*                   paystatus: 1, // 支付状态 0-未支付，1-已支付 ，9-已退款
*                   paytime: "2022-07-15 12:32", // 支付时间
*                   createtime: "2022-07-15 12:32", // 创建时间
*                ],
*              "total": 200,//总条数
*              "pagecount": 20,//总页数
*              "pagenum": 1,  // 查询第几页
*              "pagesize": 10,  // 每页显示条数
*       },
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function list(req, res) {
    let result = await orderList(req.body);
    res.json(new ReturnModel(result));
}
/**
* 获取我的活动状态
* @api {post} /order/queryone 获取我的活动状态
* @apiDescription 获取我的活动状态
* @apiName queryone
* @apiParam (参数) {String} pcode 用户code
* @apiParam (参数) {String} activeid 活动id
* @apiSampleRequest /order/queryone
* @apiGroup order-订单
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*    {
*     "pcode": '290464549598',  // 用户code
*     "activeid": 1,  // 活动id
*    }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": {
*                   pcode: '290464549598',
*                   title: "活动标题",
*                   ordercode: "订单号",
*                   price: 200,//订单金额,单位元
*                   title: "活动标题",
*                   tourl: "活动详情页面",
*                   imgurl: "活动简版图片",// 用于展示，点击跳转
*                   paystatus: 1, // 订单状态 -1： 未下单（未创建订单，显示：报名） 0-未支付（显示待支付），1-已支付(显示已报名) ，9-已退款（显示：报名）
*                   paytime: "2022-07-15 12:32", // 支付时间
*                   createtime: "2022-07-15 12:32", // 创建时间
*       },
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function queryone(req, res) {
    let result = await orderQueryone(req.body);
    res.json(new ReturnModel(result));
}
// 报名
async function signup(req, res) {
    let result = await signUp(req.body, req.connection.remoteAddress);
    res.json(new ReturnModel(result));
}

// 签到
async function signin(req, res) {
    let result = await signIn(req.body);
    res.json(new ReturnModel(result));
}
// 支付回调
async function notify_url(req, res) {
    let result = await order_notice(req.body);
    res.json({code: 0,msg:'ok'});
}
// 修改
async function edit(req, res) {
    let result = await order_edit(req.body);
    res.json(new ReturnModel(result));
}
// 查看
async function queryById(req, res) {
    let result = await orderById(req.body);
    res.json(new ReturnModel(result));
}
async function exportfile(req, res) {
    let result = await orderExport(req.body);
    if (result.code) {
        res.json(new ReturnModel(result));
    } else {
        res.setHeader('Content-Type', 'application/octet-stream');
        res.setHeader('Content-Disposition', 'attachment;filename=' + encodeURI(result.filename));
        res.status(200).send(result.buffer);
    }
}
// 支付
async function pay(req, res) {
    let result = await payto(req.body, req.connection.remoteAddress);
    res.json(new ReturnModel(result));
}

// 退款回调
async function notify_url_back(req, res) {
    await order_back(req.body);
    let result =  {code:0, msg:"ok"};
    res.json(new ReturnModel(result));
}

module.exports = function (app) {
    let base_url = '/api/order';

    app.post(base_url+'/list', list);
    app.post(base_url+'/queryone', queryone);
    app.post(base_url+'/signup', signup);
    app.post(base_url+'/signin', signin);
    app.post(base_url+'/notify_url', notify_url);
    app.post(base_url+'/edit', edit);
    app.post(base_url+'/queryById', queryById);
    app.post(base_url+'/exportfile', exportfile);
    app.post(base_url+'/pay', pay);
    app.post(base_url+'/notify_url_back', notify_url_back);
}
